/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.tutor.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.tutor.dao.LocationDAO;
import com.opgea.tutor.dao.LoginDAO;
import com.opgea.tutor.dao.StudentDAO;
import com.opgea.tutor.dao.TutorDAO;
import com.opgea.tutor.domain.entities.Address;
import com.opgea.tutor.domain.entities.Location;
import com.opgea.tutor.domain.entities.Login;
import com.opgea.tutor.domain.entities.Student;
import com.opgea.tutor.domain.entities.Tutor;
import com.opgea.tutor.domain.qualifiers.UserType;
import com.opgea.tutor.service.LoginService;
import com.opgea.tutor.web.dto.LoginDTO;

/**
 * 
 * @author Ramesh
 */
@Service
@Transactional
public class LoginServiceImpl implements LoginService {

    private Logger logger = Logger.getLogger(LoginServiceImpl.class);

    @Autowired
    private LoginDAO loginDAO;
    @Autowired
    private TutorDAO tutorDAO;
    @Autowired
    private StudentDAO studentDAO;
    @Autowired
    private LocationDAO locationDAO;

    public void setLoginDAO(LoginDAO loginDAO) {
	this.loginDAO = loginDAO;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public LoginDTO create(LoginDTO loginDTO) {
	Login login = new Login();
	loginDTO.unmarshall(login);
	login.setIsActive(Boolean.TRUE);
	if (loginDTO.getId() == 0) {
	    loginDAO.create(login);
	    this.createBasicProfile(loginDTO, login);
	} else {
	    loginDAO.update(login);
	}
	loginDTO.setId(login.getId());
	return loginDTO;
    }

    public LoginDTO update(LoginDTO loginDTO) {
	Login login = new Login();
	loginDTO.unmarshall(login);
	loginDAO.update(login);
	loginDTO.setId(login.getId());
	return loginDTO;
    }

    public LoginDTO find(Long id) {
	Login login = loginDAO.find(id);
	LoginDTO loginDTO = new LoginDTO();
	loginDTO.marshall(login);
	return loginDTO;
    }

    public LoginDTO findByLoginId(String loginId) {
	Login login = loginDAO.findByLoginId(loginId);
	LoginDTO loginDTO = new LoginDTO();
	loginDTO.marshall(login);
	return loginDTO;
    }

    public List<LoginDTO> findAll() {
	List<Login> loginList = loginDAO.findAll();
	List<LoginDTO> loginDTOList = new ArrayList<LoginDTO>();
	for (Login login : loginList) {
	    LoginDTO dto = new LoginDTO();
	    dto.marshall(login);
	    loginDTOList.add(dto);
	    logger.info(dto);
	}
	return loginDTOList;
    }

    public Map<String, Object> verify(String loginId, String password) {
	Map<String, Object> responseMap = new HashMap<String, Object>();
	responseMap.put("loginId", Boolean.TRUE);
	responseMap.put("password", Boolean.TRUE);
	responseMap.put("exception", Boolean.FALSE);

	Login login = loginDAO.findByLoginId(loginId);
	if (login == null) {
	    responseMap.put("loginId", Boolean.FALSE);
	}
	if (login != null && !login.getPassword().equals(password)) {
	    responseMap.put("password", Boolean.FALSE);
	}
	if (login != null) {
	    LoginDTO loginDTO = new LoginDTO();
	    loginDTO.marshall(login);
	    responseMap.put("object", loginDTO);
	}
	return responseMap;
    }

    private void createBasicProfile(LoginDTO loginDTO, Login login) {
	if (loginDTO.getUserType()
		.equalsIgnoreCase(UserType.Student.toString())) {
	    Student student = new Student();
	    student.setLogin(login);
	    studentDAO.create(student);
	}
	if (loginDTO.getUserType().equalsIgnoreCase(UserType.Tutor.toString())) {
	    Tutor tutor = new Tutor();
	    Address address = new Address();
	    Location location = new Location();
	    locationDAO.create(location);
	    tutor.setLocation(location);
	    tutor.setAddress(address);
	    tutor.setLogin(login);
	    tutorDAO.create(tutor);
	}
    }
}
